<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="关键字" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="手机号/邮箱"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="状态" prop="enabled">
          <el-select
            v-model="queryParams.enabled"
            placeholder="全部"
            clearable
            class="!w-[100px]"
          >
            <el-option :value="1" label="未使用" />
            <el-option :value="0" label="已使用" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button class="filter-item" type="primary" @click="handleQuery">
            <template #icon>
              <Search />
            </template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon>
              <Refresh />
            </template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-wrapper">
      <template #header>
        <el-button
          v-hasPerm="['sys:verify:code:delete']"
          type="danger"
          :disabled="ids.length === 0"
          @click="handleDelete()"
        >
          <template #icon>
            <Delete />
          </template>
          删除
        </el-button>
      </template>

      <el-table
        v-loading="loading"
        :data="tableData"
        row-key="id"
        border
        default-expand-all
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="userId" label="用户id" width="100" />
        <el-table-column
          prop="contactType"
          label="联系类型"
          width="120"
          align="center"
        >
          <template #default="scope">
            <span v-if="scope.row.contactType == 1">手机号</span>
            <span v-else-if="scope.row.contactType == 2">邮箱</span>
          </template>
        </el-table-column>
        <el-table-column prop="contact" label="联系方式" min-width="200" />
        <el-table-column
          prop="verifyCode"
          label="验证码"
          width="100"
          align="center"
        />
        <el-table-column
          prop="verifyType"
          label="验证类型"
          width="100"
          align="center"
        >
          <template #default="scope">
            <span v-if="scope.row.verifyType == 1">登陆</span>
            <span v-else-if="scope.row.verifyType == 2">绑定</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="platform"
          label="平台"
          width="100"
          align="center"
        >
          <template #default="scope">
            <span v-if="scope.row.platform == 1">后台管理</span>
            <span v-else-if="scope.row.platform == 2">商城</span>
          </template>
        </el-table-column>
        <el-table-column prop="enabled" label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.enabled == 1" type="success">未使用</el-tag>
            <el-tag v-else type="info">已使用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="expireTime"
          label="过期时间"
          width="160"
          align="center"
        />
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="160"
          align="center"
        />
        <el-table-column label="操作" fixed="right" align="left" width="100">
          <template #default="scope">
            <el-button
              v-hasPerm="['sys:verify:code:delete']"
              type="danger"
              link
              size="small"
              @click.stop="handleDelete(scope.row.id)"
            >
              <template #icon>
                <Delete />
              </template>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "VerifyCode",
  inheritAttrs: false,
});

import CodeAPI, { CodePageVO, CodePageQuery } from "@/api/tool/code";

const queryFormRef = ref(ElForm);

const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);
const tableData = ref<CodePageVO[]>();

const queryParams = reactive<CodePageQuery>({
  pageNum: 1,
  pageSize: 10,
});

/** 查询职位 */
function handleQuery() {
  loading.value = true;
  CodeAPI.getPage(queryParams)
    .then((data) => {
      tableData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function handleResetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}

/** 删除职位 */
function handleDelete(codeId?: number) {
  const codeIds = codeId ? [codeId] : ids.value;

  if (!codeIds) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    () => {
      loading.value = true;
      CodeAPI.deleteByIds(codeIds)
        .then(() => {
          ElMessage.success("删除成功");
          handleResetQuery();
        })
        .finally(() => (loading.value = false));
    },
    () => {
      ElMessage.info("已取消删除");
    }
  );
}

onMounted(() => {
  handleQuery();
});
</script>
